reconnectDevice() {
		this.reconnect();

	},
	// 1. 初始化蓝牙适配器
	initBluetooth() {
		uni.openBluetoothAdapter({
			success: (res) => {
				console.log('蓝牙适配器初始化成功', res);
				this.startDiscovery();
			},
			fail: (err) => {
				console.error('初始化失败', err);
				if (err.errCode === 10001) {
					uni.showModal({
						title: '提示',
						content: '请开启手机蓝牙功能',
						showCancel: false
					});
				}
			}
		});
	},

	// 2. 开始搜索设备
	startDiscovery() {
		uni.startBluetoothDevicesDiscovery({
			allowDuplicatesKey: false,
			success: (res) => {
				console.log('开始搜索设备', res);
				this.listenNewDevices();
			},
			fail: (err) => {
				console.error('搜索失败', err);
			}
		});
	},

	// 监听发现新设备
	listenNewDevices() {
		uni.onBluetoothDeviceFound((devices) => {

			devices.devices.forEach(device => {
				if (device.name && device.name.includes('BLE-GUC4')) {
					console.log(device);
					console.log('发现目标设备', device);
					this.deviceId = device.deviceId;
					this.connectDevice(device.deviceId);
				}
			});
		});
	},

	// 3. 连接设备
	connectDevice(deviceId) {
		this.connectedDeviceId = deviceId;
		uni.createBLEConnection({
			deviceId: this.connectedDeviceId,
			success: (res) => {
				console.log('连接成功', res);
				this.currentDeviceId = deviceId;
				this.getServices(deviceId);

				//设置传输单元
				uni.setBLEMTU({
					deviceId: this.connectedDeviceId,
					mtu: 32,
				})

			},
			fail: (err) => {
				console.error('连接失败', err);
			}
		});
	},

	// 获取设备服务
	getServices(deviceId) {
		uni.getBLEDeviceServices({
			deviceId: this.connectedDeviceId,
			success: (res) => {
				console.log(res);
				console.log('获取服务成功', res.services);
				// const serviceId = res.services[0].uuid; // 通常使用第一个服务
				// this.getCharacteristics(deviceId, serviceId);
				for (let i = 0; i < res.services.length; i++) {
					console.log("第" + (i + 1) + "个service:UUID:" + res.services[i].uuid + "\n")
					this.getCharacteristics(deviceId, res.services[i].uuid)
				}
			}
		});
		this.listenData();


	},

	// 获取特征值
	getCharacteristics(deviceId, serviceId) {
		this.serviceId = serviceId;
		uni.getBLEDeviceCharacteristics({
			deviceId: this.connectedDeviceId,
			serviceId,
			success: (res) => {
				console.log(res);
				console.log('获取特征值成功', res.characteristics);
				for (let i = 0; i < res.characteristics.length; i++) {
					if (res.characteristics[i].properties.notify == true) {
						// this.characteristicId = res.characteristics[i].uuid; 
						this.notifyBLECharacteristicValueChange(deviceId, serviceId, res
							.characteristics[i].uuid);


					}
					if (res.characteristics[i].properties.write == true) {
						if (this.writedevices == null) {
							this.writedevices = {
								"deviceId": deviceId,
								"serviceId": serviceId,
								"characteristicId": res.characteristics[i].uuid
							}
						}

					}
				}
			}
		});
	},

	// 启用特征值变化通知
	notifyBLECharacteristicValueChange(deviceId, serviceId, characteristicId) {
		console.log('deviceId', deviceId);
		console.log('serviceId', serviceId);
		console.log('characteristicId', characteristicId);
		uni.notifyBLECharacteristicValueChange({
			state: true,
			deviceId: this.connectedDeviceId,
			serviceId,
			characteristicId,
			success: (res) => {
				console.log('启用通知成功', res);
				this.listenData(); // 开始监听数据

			},
			fail: (err) => {
				console.log(err);
			}
		});
	},

	// 4. 监听接收数据
	listenData(characteristicId) {
		uni.onBLECharacteristicValueChange((res) => {
			const value = res.value;
			console.log(value);
			console.log('收到数据:', value);
			console.log('完整数据包:', value);
			console.log('起始码:', value[0]);
			console.log('结束码:', value[value.length - 1]);
			console.log('包类型:', value[1]);
			// 解析数据逻辑...
			this.checkResultData(this.ab2hex(res.value))
		});
	},


	checkResultData(value, deviceId) {
		let that = this;
		console.log('value=', value);
		let len = value.length;
		if (len < 255) {
			let frist = value[0];
			if (frist != 42) {
				console.log('起始码错误');
				return false;
			}
			let endData = value[len - 1];
			if (endData != 35) {
				console.log('结束码错误');
				return false;
			}
			let packType = value[1];
			if (packType == 1) { //发送时间

				let now = new Date();
				let yy = now.getFullYear();
				let mm = now.getMonth() + 1;
				let dd = now.getDate();
				let hh = now.getHours();
				let mi = now.getMinutes();
				let ss = now.getSeconds();
				console.log('发送时间 , 不处理', parseInt(yy % 256));
				const buffer = new ArrayBuffer(13)
				const dataView = new DataView(buffer)
				dataView.setUint8(0, 0x2a)
				dataView.setUint8(1, 1)
				dataView.setUint8(2, 8)
				dataView.setUint8(3, 0)
				dataView.setUint8(4, parseInt(yy / 256))
				dataView.setUint8(5, parseInt(yy % 256))
				dataView.setUint8(6, parseInt(mm))
				dataView.setUint8(7, parseInt(dd))
				dataView.setUint8(8, parseInt(hh))
				dataView.setUint8(9, parseInt(mi))
				dataView.setUint8(10, parseInt(ss))
				dataView.setUint8(11, that.bccCheck(dataView))
				dataView.setUint8(12, 0x23)

				console.log('buffer', buffer);
				console.log('dataView', dataView);
				console.log(that.writedevices);

				this.writeBLECharacteristicValue(buffer, that.writedevices.deviceId, that.writedevices.serviceId,
					that.writedevices.characteristicId)

				return false;
			}


			if (packType == 4) {
				console.log('倒计时 , 不处理');
				return false;
			}
			if (packType == 2) {
				console.log('设备信息帧 , 不处理');
				const buffer = new ArrayBuffer(6)
				const dataView = new DataView(buffer)
				dataView.setUint8(0, 0x2a)
				dataView.setUint8(1, 2)
				dataView.setUint8(2, 1)
				dataView.setUint8(3, 0) //成功
				dataView.setUint8(4, that.bccCheck(dataView))
				dataView.setUint8(5, 0x23)


				this.writeBLECharacteristicValue(buffer, that.writedevices.deviceId, that.writedevices.serviceId,
					that.writedevices.characteristicId)
				return false;
			}
			if (packType == 3) {
				console.log('正式尿酸数据');
				console.log('value=', value);

				var d = new Uint8Array(value.slice(12, 16));
				console.log(d.buffer);
				var dataView = new DataView(d.buffer);
				var blobLen = dataView.getInt32(0);

				console.log("测量尿酸值：" + blobLen);

				const buffer1 = new ArrayBuffer(7)
				const dataView1 = new DataView(buffer1)
				dataView1.setUint8(0, 0x2a)
				dataView1.setUint8(1, 3)
				dataView1.setUint8(2, 2)
				dataView1.setUint8(3, 0) //成功
				dataView1.setUint8(4, 0)
				dataView1.setUint8(5, that.bccCheck(dataView))
				dataView1.setUint8(6, 0x23)


				this.writeBLECharacteristicValue(buffer, that.writedevices.deviceId, that.writedevices.serviceId,
					that.writedevices.characteristicId)

				let niansuanHistoryTime = "" + value[6] + value[7] + value[8] + value[9] + value[10]

				let niansuanHistoryTimeStorage = uni.getStorageSync('niansuanHistoryTime')
				if (niansuanHistoryTime != niansuanHistoryTimeStorage) {
					uni.setStorageSync('niansuanHistoryTime', niansuanHistoryTime)
				}
			}
			console.log('return true');
			return true;
		}
		return false;
	},

	writeBLECharacteristicValue(buffer, deviceId, serviceId, characteristicId) {
		let that = this;
		console.log(JSON.stringify(buffer));
		console.log("发送数据" + buffer + '111' + deviceId + "111" + serviceId + "111" + characteristicId)
		setTimeout(() => {
			uni.writeBLECharacteristicValue({
				// 这里的 deviceId 需要在 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取
				deviceId,
				// 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
				serviceId,
				// 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取
				characteristicId,
				// 这里的value是ArrayBuffer类型
				value: buffer,
				success: function(res) {
					console.log('发包成功')
					console.log(res)
				},
				fail: function(res) {
					console.log(res)
					console.log('发包失败')
					if (res.errCode === 10007) {
						console.log('尝试重新写入...');
						that.writeBLECharacteristicValue(buffer, deviceId, serviceId,
							characteristicId); // 自动重试
					}
				},
				complete: function(res) {
					console.log('完成')
					console.log(res)
				}


			})
		}, 50)

	},


	ab2hex(buffer) {
		const hexArr = Array.prototype.map.call(
			new Uint8Array(buffer),
			function(bit) {
				// return ('00' + bit.toString(16)).slice(-2)
				return parseInt(bit.toString(10));
			}
		)
		console.log(hexArr);
		return hexArr;
	},

	//数据验证
	bccCheck(dataView) {
		let checksum = 0x00
		for (let i = 1; i < dataView.byteLength - 2; i++) {
			checksum ^= dataView.getInt8(i)
		}

		return checksum
	},



	// 5. 断开连接
	disconnect() {
		if (!this.currentDeviceId) return;

		uni.closeBLEConnection({
			deviceId: this.currentDeviceId,
			success: (res) => {
				console.log('断开连接成功', res);
				this.stopDiscovery();
			}
		});
	},

	// 停止搜索
	stopDiscovery() {
		uni.stopBluetoothDevicesDiscovery({
			success: (res) => {
				console.log('停止搜索成功', res);
			}
		});
	},

	// 6. 重新连接设备
	reconnect() {
		if (!this.currentDeviceId) return;

		this.connectDevice(this.currentDeviceId);
	},

}